<?php
/**
 * Created by PhpStorm.
 * User: ylq
 * Date: 2018/8/7
 * Time: 16:27
 */

namespace app\api\controllers;

use app\api\core\wechat\wxUser;
use Yii;
use \Cm;
use app\api\core\wechat\wechat;
use app\api\core\BaseController;
use app\api\interfaces\UserInterface;

class UserController extends BaseController implements UserInterface
{
    public function actionList()
    {
        $wxAppId = Cm::$req->post('appid');

        $wechat = new wxUser();
        $users = $wechat->getUserList($wxAppId);
        if ($users) {
            Cm::$res->susess('已更新粉丝数据');
        } else {
            Cm::$res->error($wechat->error);
        }
    }
    public function actionUser_info()
    {
        $wxAppId = Cm::$req->post('appid');
        $openid = Cm::$req->post('openid');

        $wechat = new wxUser();
        $user = $wechat->getUserInfo($wxAppId, $openid, true);
        if ($user) {
            Cm::$res->susess('成功', $user);
        } else {
            Cm::$res->error($wechat->error);
        }
    }

    public function actionSync_data()
    {
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);

        (new wxUser())->getUserList($appid);
        AnalysisController::syncAll($appid);

        Cm::$res->susess('数据同步成功！');
    }
}